import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class VendingMachineTest {

	@Before
	public void setUp() throws Exception {
	}


	@After
	public void tearDown() throws Exception {
	}
	
	
	@Test (expected=VendingMachineException.class)
	public void testGetSlotIndex() throws Exception{
		VendingMachine a = new VendingMachine();			//create a new VM object named a
		VendingMachineItem item = new VendingMachineItem("chips", 1.50);  //create a new item and give a name and price			
		a.addItem(item, "E");  // test exception
	}
	
	
	
	@Test (expected=VendingMachineException.class)
	public void testAddItem() throws Exception{
		VendingMachine a = new VendingMachine();			//create a new VM object named a
		VendingMachineItem item = new VendingMachineItem("chips", 1.50);  //create a new item and give a name and price		
		a.addItem(item, VendingMachine.A_CODE);						//test the addItem function can get the code(A,B,C,D) which defined in java file
		a.addItem(item, VendingMachine.A_CODE);	   //test if slot is occupied
		
	}
	
	@Test 
	public void testGetItem() throws Exception{
		VendingMachine a = new VendingMachine();			//create a new VM object named a
		VendingMachineItem item = new VendingMachineItem("chips", 1.50);  //create a new item and give a name and price		
		a.addItem(item, VendingMachine.A_CODE);						//test the addItem function can get the code(A,B,C,D) which defined in java file
		a.getItem("A");
	}
	

	@Test (expected=VendingMachineException.class)
	public void testRemoveItem() {
		VendingMachine a = new VendingMachine();			//create a new VM object named a
		a.removeItem("A");
		VendingMachineItem item0 = new VendingMachineItem("chips", 1.50); //create a new item and give a name and price
		VendingMachineItem item1 = new VendingMachineItem("cake", 2.50); //create a new item and give a name and price
		a.addItem(item0, VendingMachine.A_CODE);					//give a code to the item
		a.addItem(item1, VendingMachine.B_CODE);
		a.getItem("A");
		a.getItem("B");	
		a.getItem("E");  //test if getlotsIndex string code equals E
		a.removeItem("A");
		a.removeItem("B");	
		a.removeItem("E");//test remove items which have been added
	}

	@Test
	public void testInsertMoney() {		
		VendingMachine a = new VendingMachine();	//create a new VM object named a
		a.insertMoney(0.1);					//test insert money >=0			
	}
	
	@Test (expected=VendingMachineException.class)
	public void testInsertMoneyLessthanzero() {		
		VendingMachine a = new VendingMachine();	//create a new VM object named a
		a.insertMoney(-1);					//test insert money >=0			
	}

	@Test
	public void testGetBalance() {		
		VendingMachine a = new VendingMachine();	//create a new VM object named a
		a.insertMoney(1.50);		
		a.insertMoney(2);
		assertEquals(3.50, a.getBalance(), 0.0000);  //test the balance after insert money is equals the total number
	}
	

	@Test
	public void testMakePurchase() {
		VendingMachine a = new VendingMachine();			//create a new VM object named a
		VendingMachineItem item0 = new VendingMachineItem("chips", 1.50); //create a new item and give a name and price
		VendingMachineItem item1 = new VendingMachineItem("cake", 2.50); //create a new item and give a name and price
		a.addItem(item0, VendingMachine.C_CODE);					//give a code to the item
		a.addItem(item1, VendingMachine.D_CODE);
		a.getItem("C");
		a.getItem("D");
		a.insertMoney(1.50);					
		a.makePurchase("C");		
		assertEquals(0, a.getBalance(), 0.0000);	//test the money is reduced							
	}

	@Test
	public void testReturnChange() {
		VendingMachine a = new VendingMachine();			//create a new VM object named a
		VendingMachineItem item0 = new VendingMachineItem("chips", 1.50);  //create a new item and give a name and price
		VendingMachineItem item1 = new VendingMachineItem("cake", 2.50); //create a new item and give a name and price
		a.addItem(item0, VendingMachine.A_CODE);	//give a code to the item
		a.addItem(item1, VendingMachine.B_CODE);
		a.getItem("A");
		a.getItem("B");
		a.insertMoney(3.50);						
		a.makePurchase("B");
		a.returnChange();		//test returnChange
		assertEquals(0, a.getBalance(), 0.0000);	//compare the final balance is equal to 0
	}

}
